
'''
1. 在对应的视图类中实例化一个红图对象，并为红图起一个名字

api = Redprint('book')

2. 视图类中把视图函数使用红图装饰

from apps.libs.redprint import Redprint
api = Redprint('book')
@api.route('/get_user')
def get_user():
    return 'i am jackie'

3. 在api包中生成一个蓝图，并将视图类中的红图注册到蓝图上

from flask import Blueprint
from apps.api.v1 import user, book

def create_blueprint_v1():
    bp_v1 = Blueprint('v1', __name__, url_prefix='/v1')

    user.api.register(bp_v1, url_prefix='/user')
    book.api.register(bp_v1, url_prefix='/book')

    return bp_v1

4. 在主app文件中注册最终生成的蓝图

cmp_app.register_blueprint(create_blueprint_v1())

'''


class Redprint:
    def __init__(self, name):
        self.name = name
        self.mound = []

    def route(self, rule, **options):
        def decorator(f):
            self.mound.append((f, rule, options))
            return f
        return decorator

    def register(self, bp, url_prefix=None):
        if url_prefix is None:
            url_prefix = self.name
        for f, rule, options in self.mound:
            endpoint = self.name + '+' + options.pop('endpoint', f.__name__)
            bp.add_url_rule(url_prefix + rule, endpoint, f, **options)